<?php


include_once('./common.php');

$style_customizations_submitted = 0;

$process_results = "form submission not processed.";

if (array_key_exists('_submit_check', $_POST))
{
    $style_customizations_submitted = 1;
    $process_results = process_tweak_form();
    
}


/* Usage example of this class below
$properties_reader = new Properties('./properties.xml');
$properties = $properties_reader->getProperties();
print_r($properties);
$properties['theme-config']['theme-basics']['logo'] = 'updated';
$properties_reader->updateProperties($properties);
*/


function process_tweak_form(){
    //print_r ($_POST);  
    
    $SERVER_NAME = $_SERVER["SERVER_NAME"];
    list($config_instance) = split("[/.]", $SERVER_NAME, 2);
    
    //echo $SERVER_NAME . " | " . $config_instance;
    
    $base_config_dir = "/var/www/endeepen/instances/";
        
    $theme_customization_path = $base_config_dir . $config_instance . "/theme/topic.xml";
        
    $xml_config_reader = new Properties($theme_customization_path);
    $xml_config_data = $xml_config_reader->getProperties();
    
    //echo "xml config data: <br />\n";
    //print_r ($xml_config_data);
    
    
    foreach ( $_POST as $name=>$val ){
        list($marker,$group,$class,$element) = split("\|",$name,5);
        //echo "marker= " . $marker;
        if ($marker == "tweak"){
            
            echo "updating $name : $val <br />";
            $xml_config_data[$group][$class][$element]= $val;
        }
    }
    
    //echo "updated data";
    //print_r ($xml_config_data);
    $xml_config_reader->updateProperties($xml_config_data);

    
    
    return "form submisssion processed.\n";
}


function build_face_dropdown($fieldname,$currentvalue){
    $facedrop = "<select name='" . $fieldname . "'> \n";
    $facedrop .= "<option value='Arial'>Arial</option> \n";
    $facedrop .= "<option value='Georgia'>Georgia</option> \n";
    $facedrop .= "<option value='Verdana'>Verdana</option> \n";
    $facedrop .="</select> \n";
    return $facedrop;
}

function build_textfield($fieldname,$currentvalue,$width=50){
    $textfield = "<input type='text' value='" . $currentvalue . "' width=" . $width . " name='" . $fieldname . "'>";
    return $textfield;
}

function buildformelement($theelement,$thevalue,$objectname){
    $builtelement = $theelement . ": ";
    switch ($theelement) {
    case "font-color":
        $builtelement .= build_textfield($objectname,$thevalue);
        break;
    case "font-size":
        $builtelement .= build_textfield($objectname,$thevalue);
        break;
    case "font-family":
        $builtelement .= build_face_dropdown($objectname,$thevalue);
        break;
    default:
        $builtelement .= build_textfield($objectname,$thevalue);
    }
    
    return $builtelement;
}


function build_customization_form() {
    $SERVER_NAME = $_SERVER["SERVER_NAME"];
    list($config_instance) = split("[/.]", $SERVER_NAME, 2);
    
    //echo $SERVER_NAME . " | " . $config_instance;
    
    $base_config_dir = "/var/www/endeepen/instances/";
    
    $instance_path = $base_config_dir . $config_instance . "/" .  $config_instance. ".xml";
    
    $theme_customization_path = $base_config_dir . $config_instance . "/theme/topic.xml";
    
    $check_for_config = file_exists($theme_customization_path);

    $theme_customizations = "<!-- no customization found -->";

    if ($check_for_config){
        
        $xml_config_data = file_get_contents($theme_customization_path);
                
        $customization_xml = new SimpleXMLElement($xml_config_data);
        
        $custom_styles = $customization_xml->styles;
        
        
        
        $theform = "<form method='post'> \n";
        $theform .= "<input type='hidden' name='_submit_check' value='1'/>\n";
        //Add Type Treatments
        $type_treatments = $custom_styles->type_treatments;
        $i = 0;
        
        foreach ($type_treatments->children() as $typestyle)
        {
           if ($i < 1)
           {
                $theform.="<h2>Configurable Type Styles</h2>";
                $i++;
                
            }
           
           
           $stylename = trim($typestyle->getName());
           $baseformname = "tweak|type_treatments|" . $stylename;
           $i = 0;
           foreach ($typestyle->children() as $stylelement)
           {
                if ($i < 1)
                {
                     $theform.="<h3>" . $stylename . "</h3>";
                     $i++;
                     
                 }
                $theelement = trim($stylelement->getName());
                $thevalue = $stylelement;
                $objectname .=  $baseformname . "|" . $theelement;
                
                $formelement = buildformelement($theelement,$thevalue,$objectname);
                
                
                $theform .= $formelement .  "<br /> \n";
                $objectname = "";
           }
           $theform .= " \n";
           
        }
        //Add Boxes and Colors
        
        $boxesandcolor_treatments = $custom_styles->boxesandcolors;
        
        $i = 0;
        
        foreach ($boxesandcolor_treatments->children() as $bandcstyles)
        {
           if ($i < 1 )
           {
                $theform .="<h2>Boxes and Colors</h2>";
                $i++;
            }
           
           $stylename = trim($typestyle->getName());
           $baseformname = "tweak|boxesandcolors|" . $stylename;
           $i = 0;
           foreach ($typestyle->children() as $stylelement)
           {
                if ($i < 1)
                {
                     $theform.="<h3>" . $stylename . "</h3>";
                     $i++;
                     
                 }
                $theelement = trim($stylelement->getName());
                $thevalue = $stylelement ;
                $objectname .=  $baseformname . "|" . $theelement;
                
                $formelement = buildformelement($theelement,$thevalue,$objectname);
                
                
                $theform .= $formelement .  "<br /> \n";
                $objectname = "";
           }
           $theform .= " \n";
           
        }

        $theform .= "<input type='submit' value='Save style tweaks'>\n";
        $theform .= "</form> \n";
        
        $theme_customizations = $theform;


    }
    
    return $theme_customizations;
  
}



if ($style_customizations_submitted == 0)
{


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/dialogs/dialogs.css">
<title>Tweak Styles for Your Daylife Pages</title>
</head>

<body>
<img class="logo" src="../media/Daylifepages.gif" width="250" height="50" />
<div class="dialog_content">
<h1>Tweak your Daylife Pages Theme</h1>
<? echo build_customization_form(); ?>
</div>
</body>
<? }
else
{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<SCRIPT TYPE="text/javascript">
function init()
{
     window.top.hidePopWin(true);
}
//-->
</SCRIPT>
</head>
<!--body onload="init()"-->

    <? echo $process_results; ?>
    theme customizations saved.
</body>
<? } ?>

</html>       



